package com.point.business.servlets;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.google.gson.JsonObject;
import com.point.business.database.managers.InformationManager;
import com.point.business.database.model.InformationNO;
import com.point.business.parsers.ParserNO;
import com.point.business.util.ParserException;
import com.point.business.util.UTFUtil;

/**
 * Servlet implementation class LoginServlet
 */

public class ParseNOServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ParseNOServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		UTFUtil.utf8(request, response);
		try {
			List<FileItem> multiparts = new ServletFileUpload(
					new DiskFileItemFactory()).parseRequest(request);
			FileItem DSTfile = (FileItem) multiparts.get(0);
			InputStream filecontent = DSTfile.getInputStream();
			ParserNO noData = new ParserNO();
			List<InformationNO> result = noData.parse(filecontent);
			boolean isCreated = true;
			for (InformationNO informationNO : result) {
				isCreated &= InformationManager.getInstance().createNORecords(informationNO.getEvidentionNumber(), 
						informationNO.getLegalSubject(), 
						informationNO.getOdu(), 
						informationNO.getContent(), 
						informationNO.getZalozenDoveritel(), 
						informationNO.getEmbsZalozenDoveritel(), 
						informationNO.getZalozenDolznik(), 
						informationNO.getEmbsZalozenDolznik(), 
						informationNO.getDolznik(), 
						informationNO.getEmbsDolznik(), 
						informationNO.getNotar(), 
						informationNO.getEdib1(), 
						informationNO.getEdib2(),
						informationNO.getEdib3());
			}
			PrintWriter out = response.getWriter();
			JsonObject jsono = new JsonObject();
			if(isCreated){
				jsono.addProperty("success", true);
				out.print(jsono);
			}else{
				jsono.addProperty("errorMsg","Настана грешка!");
				out.print(jsono);
			}

		} catch (ParserException e) {
			JsonObject jsono = new JsonObject();
			PrintWriter out = response.getWriter();
			jsono.addProperty("errorMsg", e.getMessage());
			out.print(jsono);
			e.printStackTrace();
		} catch (FileUploadException e) {
			JsonObject jsono = new JsonObject();
			PrintWriter out = response.getWriter();
			jsono.addProperty("errorMsg", e.getMessage());
			out.print(jsono);
			e.printStackTrace();
		}

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}
}
